package com.ddd.wandemo;

import android.content.res.TypedArray;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.base.BaseContract;
import com.base.BaseSlideOutActivity;
import com.just.agentweb.AgentWeb;
import com.util.ToastUtil;


public class ArticleDetailWebView2 extends BaseSlideOutActivity {

    public static final int TYPE_ARTICLE = 10000;
    public static final int TYPE_HOME = 10001;
    private LinearLayout mLlAgenWeb;
    private AgentWeb mAgentWeb;
    private String mTitle;
    private String mLink;
    private int mType;
    private Toolbar mToolbar;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_article_detail2;
    }

    @Override
    protected void initView() {
        mLlAgenWeb = findViewById(R.id.ll_agen_web);
        mToolbar = findViewById(R.id.toolbar);
    }

    @Override
    protected void initEvent() {

        mLink = getIntent().getStringExtra("link");
        mTitle = getIntent().getStringExtra("title");
        mType = getIntent().getIntExtra("type", 0);

        initToolBar();

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(mLlAgenWeb, new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(mLink);

        WebView mWebView = mAgentWeb.getWebCreator().getWebView();
        WebSettings mSettings = mWebView.getSettings();

        mSettings.setJavaScriptEnabled(true);
        mSettings.setSupportZoom(true);
        mSettings.setBuiltInZoomControls(true);
        //不显示缩放按钮
        mSettings.setDisplayZoomControls(false);
        //设置自适应屏幕，两者合用
        //将图片调整到适合WebView的大小
        mSettings.setUseWideViewPort(true);
        //缩放至屏幕的大小
        mSettings.setLoadWithOverviewMode(true);
        //自适应屏幕
        mSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }


    private void initToolBar() {
        mToolbar.setTitle(mTitle);
        //根据主题设置返回键
        TypedArray array = getTheme().obtainStyledAttributes(new int[] {
                R.attr.back_icon
        });
        int backIcon = array.getResourceId(0, 0);

        mToolbar.setNavigationIcon(backIcon);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //设置右上角menu
        if (mType == TYPE_ARTICLE) {
            getMenuInflater().inflate(R.menu.normal_toolbar_menu, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    //toolbar按钮点击时间
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_collect:
                ToastUtil.showShort(ArticleDetailWebView2.this, item.getTitle());
                break;
            case R.id.action_share:
                ToastUtil.showShort(ArticleDetailWebView2.this, item.getTitle());
                break;
            case R.id.action_copy_link:
                ToastUtil.showShort(ArticleDetailWebView2.this, item.getTitle());
                break;
            case R.id.action_open_link:
                ToastUtil.showShort(ArticleDetailWebView2.this, item.getTitle());
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected BaseContract.RootPresenter initPresenter() {
        return null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return mAgentWeb.handleKeyEvent(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }
}
